﻿using System;
using System.Collections.Generic;

using System.Text;

namespace System.Qizx.Xml
{
    /*******************************/
    /// <summary>
    /// Emulates XmlFilter behaviors over piped transformations.
    /// </summary>
    public class TransformXmlFilterSupport : XmlSaxXMLFilter
    {
        private TransformerSupport transformer;
        private BasicSource XslSource;
        private XmlSAXDocumentManager parent;


        /// <summary>
        /// Creates a new instance of TransformXmlFilterSupport from a BasicSourceSupport.
        /// </summary>
        /// <param name="source">The BasicSourceSupport instance to be used.</param>
        public TransformXmlFilterSupport(BasicSource source)
        {
            XslSource = source;
            transformer = new TransformerSupport();
            transformer.Load(source);
        }


        /// <summary>
        /// This method is used for Piped transformations using the XmlReaders instance.
        /// </summary>
        /// <param name="Source">The XmlSource with the XML document.</param>
        /// <param name="Filter">The TransformXmlFilterSupport for piped Transformations.</param>
        /// <returns>An XmlSource instance</returns>
        public virtual XmlSource Parse(XmlSource Source, TransformXmlFilterSupport Filter)
        {
            if (Filter.getParent() is TransformXmlFilterSupport)
            {
                System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
                GenericResult tempResult = new GenericResult(tempStream);
                SaxSource SaxSource = new SaxSource(Parse(Source, (TransformXmlFilterSupport)Filter.getParent()));
                Filter.transformer.DoTransform(SaxSource, tempResult);
                return new XmlSource(tempStream);
            }
            else
            {
                Filter.getParent().parse(Source);
                System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
                GenericResult tempResult = new GenericResult(tempStream);
                SaxSource SaxSource = new SaxSource(Source);
                Filter.transformer.DoTransform(SaxSource, tempResult);
                Filter.parse(tempStream);
                tempStream.Position = 0;
                return new XmlSource(tempStream);
            }
        }

        /// <summary>
        /// This method overrides the method parse of the XmlSaxXMLFilter class.
        /// </summary>
        /// <param name="filepath">A string with the file path of the XML source.</param>
        public override void parse(String filepath)
        {
            base.parse(Parse(new XmlSource(filepath), this));
        }

        /// <summary>
        /// This method overrides the method parse of the XmlSaxXMLFilter class.
        /// </summary>
        /// <param name="source">The XmlSource instance with the Xml source.</param>
        public override void parse(XmlSource source)
        {
            base.parse(Parse(source, this));
        }

        /// <summary>
        /// Overrides the SetParent method of the XmlSaxXMLFilter.
        /// </summary>
        /// <param name="parent">A XmlSAXDocumentManager instance that is parent of this instance.</param>
        public override void setParent(XmlSAXDocumentManager parent)
        {
            this.parent = parent;
        }

        /// <summary>
        /// Overrides the GetParent method of the XmlSaxXMLFilter.
        /// </summary>
        /// <returns>A XmlSAXDocumentManager instance that is parent of this instance.</returns>
        public override XmlSAXDocumentManager getParent()
        {
            return this.parent;
        }
    }
}
